package keshe;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import org.jetbrains.annotations.NotNull;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class PassStudents extends IndexOrManage implements ActionListener{

    String[] subjects={"","语文","英语","数学"};
    JComboBox subject=new JComboBox(subjects);
    JLabel la=new JLabel("选择查询科目");

    public PassStudents(){
        super();
        p1.add(la);
        p1.add(subject);
        //注册监听
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);

        pack();
        setVisible(true);
    }
    @Override
    public void actionPerformed(@NotNull ActionEvent e) {
        // TODO 自动生成的方法存根
        if(e.getActionCommand()=="显示"){
            text.setText("");
            String s="";
            if(subject.getSelectedItem().equals("语文"))
                s="Chinese";
            else if(subject.getSelectedItem().equals("英语"))
                s="English";
            else if(subject.getSelectedItem().equals("数学"))
                s="Maths";
            else
                JOptionPane.showMessageDialog(null, "找不到该科目", "Message",
                        JOptionPane.INFORMATION_MESSAGE);
            if(s.equals("")){

            }
            else
                try{
                    Connection con=DataBaseCreate.getConnection();
                    Statement sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
                    ResultSet rs=sql.executeQuery("select * from student where "+s+">=60");
                    text.append(subject.getSelectedItem()+"及格学生:\r\n");
                    text.append("学号                姓名                性别                专业                年级                班别                成绩        \r\n");
                    while(rs.next()){
                        text.append(rs.getString("Num")+"           "+rs.getString("Name")+"           "+rs.getString("Sex")+"           "+rs.getString("Major")
                                +"           "+rs.getString("Grade")+"                      "+rs.getString("Class")+
                                "                      "+rs.getFloat(s)+"\r\n");
                    }

                }catch(java.lang.ClassNotFoundException err){
                    JOptionPane.showMessageDialog(null, err.getMessage(), "ClassNotFoundException:",
                            JOptionPane.INFORMATION_MESSAGE);
                }catch (SQLException ex){
                    JOptionPane.showMessageDialog(null, ex.getMessage(), "SQLException: ", JOptionPane.INFORMATION_MESSAGE);
                }

        }
        else if(e.getActionCommand()=="取消"){
            setVisible(false);
        }
        else if(e.getActionCommand()=="保存"){
            TxtSave save=new TxtSave(text.getText(),subject.getSelectedItem()+"合格");
        }
    }

}
